package com.wwe.kotlinstudy.util

/**
 * @name KotlinStudy
 * @package name：com.wwe.kotlinstudy.util
 * @anthor DavidZhang
 * @time 2018/4/17 15:37
 * @class describe
 */
class StringStudy {

    fun charToInt(char: Char): Int {
        if(char !in '1'..'9') {
            throw IllegalArgumentException("Out of range")
        }
        return char.toInt() - '0'.toInt() //显示转换为数字
    }

    /**原生字符串*/
    fun test1() {
        val text = """
          for (c in "foo")
              print(c)
        """
    }

    /**在Kotlin中默认 | 用作边界前缀，但你可以选择其他字符并作为参数传入，比如 trimMargin(">")。你也可以通过 trimMargin() 函数去除前导空格*/
    fun test2() {
        val text = """
          |Tell me and I forget.
          |Teach me and I remember.
          |Involve me and I learn.
          |(Benjamin Franklin)
        """.trimMargin()
    }

    /**字符串模板*/
    fun test3() {
        val s = "abc"
        val str = "$s.length is ${s.length}" // 求值结果为 "abc.length is 3"
    }

    fun test4() {
        var a = 1
        val s1 = "a is $a"
        a = 100
        val s2 = "${s1.replace("is", "was")},but now is $a"
    }

    fun getStringLength(obj: Any): Int? {
        if (obj is String && obj.length > 0) {
            return obj.length
        }
        return null
    }

}